package com.uml.youmath;

/*
 * @author Greater Good <http://code.google.com/p/youmath-umass-lowell/>
 * */

import java.util.HashMap;
import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

/**
 * The YouMath main Activity class. This view is defined as our starting point
 * into the app, where the user can then branch to New Game/Continue, Quick
 * Play, Options, and Leaderboard. TODO: Add Social networking things here?
 * 
 */
public class YouMath extends Activity implements OnClickListener {
	ImageButton btn_gameStart, btn_options, btn_quickPlay, btn_leaderboard;
	int currentPos = 0;
	int game, stage;
	long seed;
	Random generator;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		btn_gameStart = (ImageButton) findViewById(R.id.btn_startGame);
		btn_options = (ImageButton) findViewById(R.id.btn_options);
		btn_quickPlay = (ImageButton) findViewById(R.id.btn_quickPlay);
		btn_leaderboard = (ImageButton) findViewById(R.id.btn_leaderBoard);

		btn_gameStart.setOnClickListener(this);
		btn_options.setOnClickListener(this);
		btn_quickPlay.setOnClickListener(this);
		btn_leaderboard.setOnClickListener(this);
		
		seed = System.currentTimeMillis();
		generator = new Random(seed+=1);
	}

	@Override
	public void onPause() {
		currentPos = SoundService.musicPlayer.getCurrentPosition();
		SoundService.currentPos = currentPos;
		SoundService.musicPlayer.pause();
		super.onPause();
	}

	@Override
	public void onResume() {
		if (currentPos > 0) {
			SoundService.currentPos = currentPos;
		}
		SoundService.start(this, SoundService.MUSIC);
		super.onResume();
	}

	public void onClick(View v) {
		if (v == btn_gameStart) {
			// Go to game type selection (addition, subtraction, etc)
			Intent i = new Intent(YouMath.this, GameTypeSelect.class);
			startActivity(i);
		}
		if (v == btn_options) {
			Intent i = new Intent(YouMath.this, Options.class);
			startActivityForResult(i, 0);
		}
		if (v == btn_quickPlay) {
			game = generator.nextInt(5);
		    stage = generator.nextInt(4);
		    
			// skip to game play
			Intent i = new Intent(YouMath.this, NewGame.class);
			i.putExtra("gametype", game);
			i.putExtra("stageselect", stage);
			startActivity(i);
		}
		if (v == btn_leaderboard) {
			Intent i = new Intent(YouMath.this, ScoreScreen.class);
			i.putExtra("Score", -1);
			i.putExtra("stageSelect", -1);
			startActivity(i);
		}
	}
}